///
/// 张诚宇
///
/// 首页
///
/// 2022.6.14
///

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';
import 'package:zhangchengyu20220614/view/mypage.dart';
import 'package:zhangchengyu20220614/view/newshome.dart';

class HomePages extends StatefulWidget {
  const HomePages({Key key}) : super(key: key);

  @override
  _HomePagesState createState() => _HomePagesState();
}

class _HomePagesState extends State<HomePages> {

  bool _isOne=false;
  int _currentIndex=0;
  List<Widget> _list;
  PageController _pageController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    UmengCommonSdk.initCommon('62a8509a05844627b5b091a3', '62a8509a05844627b5b091a3', 'Umeng');

    isOneLogin();
    _pageController = PageController();
    _list=[
      NewsHomePage(),
      Container(child: Center(child: Text('视频')),),
      Container(child: Center(child: Text('发现')),),
      MyPage(),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        onPageChanged: (value){
          setState(() {
            _currentIndex=value;
          });
        },
        children: _list,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (value){
          UmengCommonSdk.onEvent('monthtap', {'userId':1});
          setState(() {
            _currentIndex=value;
          });
          _pageController.jumpToPage(_currentIndex);
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.add_to_home_screen),label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.video_collection),label: '视频'),
          BottomNavigationBarItem(icon: Icon(Icons.find_in_page_sharp),label: '发现'),
          BottomNavigationBarItem(icon: Icon(Icons.person),label: '未登录'),
        ],
      ),
    );
  }

  void isOneLogin() async{
    var sharedPreferences = await SharedPreferences.getInstance();
    var bool = sharedPreferences.getBool('isOne');

    setState(() {
      if(bool==null){
        getShowDialog();
      }else {
        _isOne=true;
      }
    });
  }



  void setSp() async{
    var sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setBool('isOne', true);
    Navigator.pop(context);
  }

  void getShowDialog() {
    showDialog(context: context, builder: (context){
      return Dialog(
        child: Container(
          width: 150,
          height: 260,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('欢迎使用新浪新闻',style: TextStyle(color: Colors.black,fontSize: 18),),
                    Text('使用前请认真阅读使用前请认真阅读使用前请认真阅读使用前请认真阅读使用前请认真阅读使用前请认真阅读使用前请认真阅读'
                      ,style: TextStyle(color: Colors.grey,fontSize: 16),
                    ),
                  ],
                ),
              ),
              TextButton(onPressed: (){
                Navigator.pop(context);
              }, child: Text("仅浏览")),
              Container(
                child: RaisedButton(onPressed: (){
                  setSp();
                }, child: Text("同意并继续"),color: Colors.deepOrange,),
              ),

            ],
          ),
        ),
      );
    });
  }
}
